package com.example.musicplayer.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.musicplayer.R;
import com.example.musicplayer.musicTools.Lyric;

public class MusicLyricAdapter extends BaseAdapter {
    private final Context mContext;
    //记录正在播放的歌词在列表的索引
    private int mNowPosition;

    public MusicLyricAdapter(Context context, int nowPosition) {
        mContext = context;
        mNowPosition = nowPosition;
    }

    //设置正在播放的歌词的索引
    public void setNowPosition(int position) {
        mNowPosition = position;
    }

    //得到正播放的歌词的索引
    public int getNowPosition() {
        return mNowPosition;
    }

    @Override
    public int getCount() {
        return Lyric.getSize();
    }

    @Override
    public Object getItem(int position) {
        return Lyric.getLyric(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.music_play_tab3_lyric_item, null);
            viewHolder.mTextViewLyric = convertView.findViewById(R.id.textview_music_play_tab3_item_lyric);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (MusicLyricAdapter.ViewHolder) convertView.getTag();
        }
        if (position == mNowPosition) {
            viewHolder.mTextViewLyric.setTextSize(Lyric.getLyricTextSize(true));
            viewHolder.mTextViewLyric.setTextColor(mContext.getResources().getColor(R.color.white));
            viewHolder.mTextViewLyric.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        } else {
            viewHolder.mTextViewLyric.setTextSize(Lyric.getLyricTextSize(false));
            viewHolder.mTextViewLyric.setTextColor(mContext.getResources().getColor(R.color.silver));
            viewHolder.mTextViewLyric.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        }

        viewHolder.mTextViewLyric.setText(Lyric.getLyric(position));
        return convertView;
    }

    //定义一个类来加载控件，来保存控件的实例化
    static class ViewHolder {
        TextView mTextViewLyric;
    }
}